package com.manli.api.service;

import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.WechatActivationCouponBatchDao;
import com.manli.api.dto.WechatActivationCouponBatch;


@Service("wechatActivationCouponBatchService")
public class WechatActivationCouponBatchService {
    @Autowired
    WechatActivationCouponBatchDao wechatActivationCouponBatchDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from wechatActivationCouponBatch where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<WechatActivationCouponBatch> pageQuery = new PageQuery<WechatActivationCouponBatch>(params.getPage(),params.getLimit());
        PageQuery<WechatActivationCouponBatch> query =sqlManager.execute(sqlReady,WechatActivationCouponBatch.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<WechatActivationCouponBatch> list = wechatActivationCouponBatchDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer id) {
        WechatActivationCouponBatch wechatActivationCouponBatch = wechatActivationCouponBatchDao.single(id);
        return ResultUtil.success(wechatActivationCouponBatch);
    }

    @Transactional
    public Result save(WechatActivationCouponBatch wechatActivationCouponBatch) {
        wechatActivationCouponBatchDao.insert(wechatActivationCouponBatch);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(WechatActivationCouponBatch wechatActivationCouponBatch) {
        wechatActivationCouponBatchDao.updateTemplateById(wechatActivationCouponBatch);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(WechatActivationCouponBatch wechatActivationCouponBatch) {
        wechatActivationCouponBatchDao.updateTemplateById(wechatActivationCouponBatch);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            wechatActivationCouponBatchDao.deleteById(id);
        }
        return ResultUtil.success();
    }


    public Result getCouponInfoById(Integer id) {
        String sql = " " +
                "select * from ( " +
                "SELECT " +
                " a.*,b.branch_head_img head_img,b.branch_name shop_name,b.branch_id as branch_id,null as partner_id,b.longitude,b.latitude,if(a.user_get_num is not null,round(a.user_get_num/a.max_coupons*100,0),100) coupon_got " +
                "FROM " +
                " wechat_activation_coupon_batch a " +
                " LEFT JOIN (select f.* from (select * from major_branch_partner) e left join branch f on e.branch_id = f.branch_id ) b ON a.wechat_mchid = b.branch_wechat_mch_id  " +
                "WHERE " +
                " a.user_get_num < a.max_coupons " +
                "union all  " +
                "SELECT " +
                " a.*,b.partner_head_img head_img,b.partner_name shop_name,null as branch_id,b.partner_id,b.longitude,b.latitude,if(a.user_get_num is not null,round(a.user_get_num/a.max_coupons*100,0),100) coupon_got " +
                "FROM " +
                " wechat_activation_coupon_batch a " +
                " LEFT JOIN (select f.* from (select * from major_branch_partner) e left join partner f on e.partner_id = f.partner_id ) b ON a.wechat_mchid = b.partner_wechat_mch_id " +
                "WHERE " +
                " a.user_get_num < a.max_coupons " +
                ") d where d.shop_name is not null and d.id = ?";
        SQLReady sqlReady = new SQLReady(sql,id);
        List<Map> list = sqlManager.execute(sqlReady, Map.class);
        return ResultUtil.success(list.size() > 0?list.get(0):null);
    }
}
